﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ControlLibrary.Win32;

namespace ControlLibrary.Utility
{
    public class ControlUtility
    {
        /// <summary>
        /// This function captures the contents of a window or control
        /// </summary>
        /// <param name="control"></param>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static bool CaptureWindow(Control control, Bitmap bitmap)
        {
            Graphics graphics = Graphics.FromImage(bitmap);

            var meptr = new IntPtr(PRF.PRF_CLIENT | PRF.PRF_ERASEBKGND | PRF.PRF_OWNED);

            IntPtr hdc = graphics.GetHdc();

            UnsafeMethods.SendMessage(control.Handle, WM.WM_PRINT, hdc, meptr);

            graphics.ReleaseHdc(hdc);

            graphics.Dispose();

            return true;
        }
    }
}
